package pers.xiaojun.boot.module.system.dao.mapper.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.module.system.controller.user.vo.user.UserPageReqVO;
import pers.xiaojun.boot.module.system.dao.entity.user.AdminUserDO;
import pers.xiaojun.boot.mybatis.core.query.LambdaQueryWrapperX;

/**
 * 描述
 *
 * @author xiaojun
 * @since 2025-10-03
 */
@Mapper
public interface AdminUserMapper extends BaseMapper<AdminUserDO> {

    /**
     * 根据用户名获取用户
     *
     * @param username 用户名
     * @return 用户
     */
    default AdminUserDO selectByUsername(String username) {
        return selectOne(new LambdaQueryWrapper<AdminUserDO>()
                .eq(AdminUserDO::getUsername, username)
        );
    }

    /**
     * 根据邮箱获取用户
     *
     * @param email 邮箱
     * @return 用户
     */
    default AdminUserDO selectByEmail(String email) {
        return selectOne(new LambdaQueryWrapper<AdminUserDO>()
                .eq(AdminUserDO::getEmail, email)
        );
    }

    /**
     * 根据手机号获取用户
     *
     * @param mobile 手机号
     * @return 用户
     */
    default AdminUserDO selectByMobile(String mobile) {
        return selectOne(new LambdaQueryWrapper<AdminUserDO>()
                .eq(AdminUserDO::getMobile, mobile)
        );
    }

    /**
     * 获取用户分页数据（条件查询）
     *
     * @param pageVO 查询条件
     * @return 分页数据
     */
    default Page<AdminUserDO> selectByPage(UserPageReqVO pageVO) {
        return this.selectPage(PageConvert.INSTANCE.toPage(pageVO), new LambdaQueryWrapperX<AdminUserDO>()
                .likeIfPresent(AdminUserDO::getUsername, pageVO.getUsername())
                .likeIfPresent(AdminUserDO::getMobile, pageVO.getMobile())
                .eqIfPresent(AdminUserDO::getStatus, pageVO.getStatus())
                .eqIfPresent(AdminUserDO::getDeptId, pageVO.getDeptId())
                .betweenIfPresent(AdminUserDO::getCreateTime, pageVO.getCreateTime())
        );
    }

}
